package datastruct.list;

/**
 * @Author Gorchun
 * @create 2021/3/18 16:10
 */
public class Utils {

    public int[] arr = new int[]{1,2,3,4,5};
    public ListNode createListNode(){
        ListNode realHead = new ListNode();
        ListNode head = realHead;
        for (int i = 0; i < arr.length; i++) {
            ListNode tmp = new ListNode(arr[i]);
            head.next = tmp;
            head = head.next;
        }
        return realHead.next;
    }

    public void showList(ListNode head){
        while (head != null){
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ListNode head = new Utils().createListNode();
        new Utils().showList(head);
    }

    /**
     * 非头插法反转链表
     * @param head
     * @return
     */
    public static ListNode reverse(ListNode head){
        ListNode newHead = null; //已反转好的链表的头节点
        ListNode next = null; //未反转好的链表的下一个节点
        while (head!=null){
            next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
        }
        return newHead;
    }
}
